package com.zzrc.grape.service.impl;

import com.google.gson.Gson;
import com.zzrc.grape.model.testTool.EnDecrypReq;
import com.zzrc.grape.service.TestToolService;
import com.zzrc.grape.util.BasePageData;
import com.zzrc.grape.util.BaseResponseData;
import com.zzrc.grape.util.HttpClientUtils;
import com.zzrc.grape.util.WebResponseCode;
import org.springframework.stereotype.Service;

import java.lang.reflect.Array;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class TestToolServiceImpl implements TestToolService {
    @Override
    public BaseResponseData enDecryp(EnDecrypReq enDecrypReq) {
        BaseResponseData data = new BaseResponseData();
        String result = null;
        Map<String, Object> params = new HashMap<>();
        List<String> values = new ArrayList<>();
        values.add(enDecrypReq.getEnDecrypStr());
        if (enDecrypReq.getOperation().equals("encryption")) {
            params.put("encrypt", true);
        } else {
            params.put("encrypt", false);
        }

        params.put("values", values);
        try {
            result = HttpClientUtils.doPostJson("http://ebjqa.zzrcer.com/peony/aksk/shared/enDecrypt", new Gson().toJson(params));
        } catch (Exception e) {
            e.printStackTrace();
        }

        if (result == null) {
            data.setCode(WebResponseCode.ERROR);
            data.setMessage(WebResponseCode.FAILINFO);
        } else {
            // 处理请求结果
            Map map = new Gson().fromJson(result, Map.class);
            System.out.println(map.toString());
            Double code = (Double) map.get("code");
            data.setCode(code.intValue());
            data.setMessage((String) map.get("message"));
            data.setBody((Map) map.get("body"));
        }
        return data;
    }
}
